using MicroMsg.Common.Utils;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
namespace MicroMsg.UI.UserContrl
{
	public class NinePatchHelper
	{
		private int TopStart;
		private int TopEnd;
		private int RightStart;
		private int RightEnd;
		private int BottomStart;
		private int BottomEnd;
		private int LeftStart;
		private int LeftEnd;
		private static Dictionary<string, NinePatchSrc[]> ninePatchDic;
		private void GetAllPos(WriteableBitmap writeableBitmap, int int_0, int int_1, ref int TopStart, ref int TopEnd, ref int RightStart, ref int RightEnd, ref int BottomStart, ref int BottomEnd, ref int LeftStart, ref int LeftEnd)
		{
			TopStart = int_0;
			RightStart = int_1;
			BottomStart = int_0;
			LeftStart = int_1;
			int num = int_0 * (int_1 - 1);
			for (int i = 0; i < int_0; i++)
			{
				byte[] bytes = BitConverter.GetBytes(writeableBitmap.get_Pixels()[i]);
				int num2 = (int)bytes[0];
				int num3 = (int)bytes[1];
				int num4 = (int)bytes[2];
				int num5 = (int)bytes[3];
				if (num2 == 0 && num3 == 0 && num4 == 0 && num5 == 255)
				{
					if (i > TopEnd)
					{
						TopEnd = i;
					}
					if (i < TopStart)
					{
						TopStart = i;
					}
				}
				bytes = BitConverter.GetBytes(writeableBitmap.get_Pixels()[num + i]);
				num2 = (int)bytes[0];
				num3 = (int)bytes[1];
				num4 = (int)bytes[2];
				num5 = (int)bytes[3];
				if (num2 == 0 && num3 == 0 && num4 == 0 && num5 == 255)
				{
					if (i > BottomEnd)
					{
						BottomEnd = i;
					}
					if (i < BottomStart)
					{
						BottomStart = i;
					}
				}
			}
			for (int j = 0; j < int_1; j++)
			{
				byte[] bytes2 = BitConverter.GetBytes(writeableBitmap.get_Pixels()[j * int_0]);
				int num6 = (int)bytes2[0];
				int num7 = (int)bytes2[1];
				int num8 = (int)bytes2[2];
				int num9 = (int)bytes2[3];
				if (num6 == 0 && num7 == 0 && num8 == 0 && num9 == 255)
				{
					if (j > LeftEnd)
					{
						LeftEnd = j;
					}
					if (j < LeftStart)
					{
						LeftStart = j;
					}
				}
				bytes2 = BitConverter.GetBytes(writeableBitmap.get_Pixels()[j * int_0 + int_0 - 1]);
				num6 = (int)bytes2[0];
				num7 = (int)bytes2[1];
				num8 = (int)bytes2[2];
				num9 = (int)bytes2[3];
				if (num6 == 0 && num7 == 0 && num8 == 0 && num9 == 255)
				{
					if (j > RightEnd)
					{
						RightEnd = j;
					}
					if (j < RightStart)
					{
						RightStart = j;
					}
				}
			}
		}
		public NinePatchSrc[] GetNinePathSrc(string imgSrc)
		{
			Log.smethod_5(base.GetType().get_Name(), "start get nine patch:" + imgSrc);
			NinePatchSrc[] array = new NinePatchSrc[9];
			for (int i = 0; i < 9; i++)
			{
				array[i] = new NinePatchSrc();
			}
			Uri uri = new Uri(imgSrc, 2);
			StreamResourceInfo resourceStream = Application.GetResourceStream(uri);
			if (resourceStream == null)
			{
				return array;
			}
			BitmapImage bitmapImage = new BitmapImage();
			bitmapImage.SetSource(resourceStream.get_Stream());
			Image image = new Image();
			image.set_Source(bitmapImage);
			image.set_Stretch(0);
			if (bitmapImage.get_PixelWidth() != 0 && bitmapImage.get_PixelHeight() != 0)
			{
				image.set_Width((double)bitmapImage.get_PixelWidth());
				image.set_Height((double)bitmapImage.get_PixelHeight());
				int pixelWidth = bitmapImage.get_PixelWidth();
				int pixelHeight = bitmapImage.get_PixelHeight();
				WriteableBitmap writeableBitmap = new WriteableBitmap((int)image.get_Width(), (int)image.get_Height());
				writeableBitmap.Render(image, null);
				writeableBitmap.Invalidate();
				this.GetAllPos(writeableBitmap, writeableBitmap.get_PixelWidth(), writeableBitmap.get_PixelHeight(), ref this.TopStart, ref this.TopEnd, ref this.RightStart, ref this.RightEnd, ref this.BottomStart, ref this.BottomEnd, ref this.LeftStart, ref this.LeftEnd);
				double num = 1.0;
				double num2 = 1.0;
				double num3 = (double)this.TopStart - num;
				double num4 = (double)this.LeftStart - num2;
				array[0].writeableBitmap_0 = this.CutImage(writeableBitmap, num, num2, num3, num4);
				array[0].double_0 = num3;
				array[0].double_1 = num4;
				num = (double)this.TopStart;
				num2 = 1.0;
				num3 = (double)(this.TopEnd - this.TopStart + 1);
				num4 = (double)(Math.Max(this.LeftStart, this.RightStart) - 1);
				array[1].writeableBitmap_0 = this.CutImage(writeableBitmap, num, num2, num3, num4);
				array[1].double_0 = num3;
				array[1].double_1 = num4;
				num = (double)(this.TopEnd + 1);
				num2 = 1.0;
				num3 = (double)(pixelWidth - this.TopEnd - 1 - 1);
				num4 = (double)(this.RightStart - 1);
				array[2].writeableBitmap_0 = this.CutImage(writeableBitmap, num, num2, num3, num4);
				array[2].double_0 = num3;
				array[2].double_1 = num4;
				num = 1.0;
				num2 = (double)this.LeftStart;
				num3 = (double)(Math.Max(this.TopStart, this.BottomStart) - 1);
				num4 = (double)(this.LeftEnd - this.LeftStart + 1);
				array[3].writeableBitmap_0 = this.CutImage(writeableBitmap, num, num2, num3, num4);
				array[3].double_0 = num3;
				array[3].double_1 = num4;
				num = (double)Math.Max(this.TopStart, this.BottomStart);
				num2 = (double)Math.Max(this.LeftStart, this.RightStart);
				num3 = (double)Math.Min(this.TopEnd, this.BottomEnd) - num + 1.0;
				num4 = (double)Math.Min(this.LeftEnd, this.RightEnd) - num2 + 1.0;
				array[4].writeableBitmap_0 = this.CutImage(writeableBitmap, num, num2, num3, num4);
				array[4].double_0 = num3;
				array[4].double_1 = num4;
				num = (double)(Math.Min(this.TopEnd, this.BottomEnd) + 1);
				num2 = (double)this.RightStart;
				num3 = (double)pixelWidth - num - 1.0;
				num4 = (double)this.RightEnd - num2 + 1.0;
				array[5].writeableBitmap_0 = this.CutImage(writeableBitmap, num, num2, num3, num4);
				array[5].double_0 = num3;
				array[5].double_1 = num4;
				num = 1.0;
				num2 = (double)(this.LeftEnd + 1);
				num3 = (double)(this.BottomStart - 1);
				num4 = (double)(pixelHeight - (this.LeftEnd + 1) - 1);
				array[6].writeableBitmap_0 = this.CutImage(writeableBitmap, num, num2, num3, num4);
				array[6].double_0 = num3;
				array[6].double_1 = num4;
				num = (double)this.BottomStart;
				num2 = (double)(Math.Min(this.LeftEnd, this.RightEnd) + 1);
				num3 = (double)(this.BottomEnd - this.BottomStart + 1);
				num4 = (double)pixelHeight - num2 - 1.0;
				array[7].writeableBitmap_0 = this.CutImage(writeableBitmap, num, num2, num3, num4);
				array[7].double_0 = num3;
				array[7].double_1 = num4;
				num = (double)(this.BottomEnd + 1);
				num2 = (double)(this.RightEnd + 1);
				num3 = (double)pixelWidth - num - 1.0;
				num4 = (double)pixelHeight - num2 - 1.0;
				array[8].writeableBitmap_0 = this.CutImage(writeableBitmap, num, num2, num3, num4);
				array[8].double_0 = num3;
				array[8].double_1 = num4;
				Log.smethod_4(base.GetType().get_Name(), "end get nine patch");
				return array;
			}
			return array;
		}
		private WriteableBitmap CutImage(WriteableBitmap writeableBitmap_0, double double_0, double double_1, double width, double height)
		{
			if (width >= 0.0 && height >= 0.0)
			{
				Image image = new Image();
				image.set_Stretch(0);
				UIElement arg_51_0 = image;
				TranslateTransform translateTransform = new TranslateTransform();
				translateTransform.set_X(-1.0 * double_0);
				translateTransform.set_Y(-1.0 * double_1);
				arg_51_0.set_RenderTransform(translateTransform);
				image.set_Source(writeableBitmap_0);
				Canvas canvas = new Canvas();
				canvas.set_Width(width);
				canvas.set_Height(height);
				canvas.get_Children().Add(image);
				return new WriteableBitmap(canvas, null);
			}
			return null;
		}
		public static byte[] GetSaveBuffer(WriteableBitmap bitmap)
		{
			if (bitmap == null)
			{
				return null;
			}
			long num = (long)(bitmap.get_PixelWidth() * bitmap.get_PixelHeight());
			long num2 = num * 4L + 4L;
			byte[] array = new byte[num2];
			byte[] arg_4C_0 = array;
			long expr_33 = 0L;
			long num3 = expr_33 + 1L;
			arg_4C_0[(int)checked((IntPtr)expr_33)] = (byte)(bitmap.get_PixelWidth() / 256 & 255);
			byte[] arg_70_0 = array;
			long expr_57 = 1L;
			num3 = expr_57 + 1L;
			arg_70_0[(int)checked((IntPtr)expr_57)] = (byte)(bitmap.get_PixelWidth() % 256 & 255);
			byte[] arg_94_0 = array;
			long expr_7B = 2L;
			num3 = expr_7B + 1L;
			arg_94_0[(int)checked((IntPtr)expr_7B)] = (byte)(bitmap.get_PixelHeight() / 256 & 255);
			byte[] arg_B8_0 = array;
			long expr_9F = 3L;
			num3 = expr_9F + 1L;
			arg_B8_0[(int)checked((IntPtr)expr_9F)] = (byte)(bitmap.get_PixelHeight() % 256 & 255);
			int num4 = 0;
			while ((long)num4 < num)
			{
				byte[] arg_D7_0 = array;
				long expr_BF = num3;
				num3 = expr_BF + 1L;
				arg_D7_0[(int)checked((IntPtr)expr_BF)] = (byte)(bitmap.get_Pixels()[num4] >> 24 & 255);
				byte[] arg_F2_0 = array;
				long expr_DA = num3;
				num3 = expr_DA + 1L;
				arg_F2_0[(int)checked((IntPtr)expr_DA)] = (byte)(bitmap.get_Pixels()[num4] >> 16 & 255);
				byte[] arg_10C_0 = array;
				long expr_F5 = num3;
				num3 = expr_F5 + 1L;
				arg_10C_0[(int)checked((IntPtr)expr_F5)] = (byte)(bitmap.get_Pixels()[num4] >> 8 & 255);
				byte[] arg_124_0 = array;
				long expr_10F = num3;
				num3 = expr_10F + 1L;
				arg_124_0[(int)checked((IntPtr)expr_10F)] = (byte)(bitmap.get_Pixels()[num4] & 255);
				num4++;
			}
			return array;
		}
		public static WriteableBitmap GetImage(byte[] buffer)
		{
			if (buffer == null)
			{
				return null;
			}
			int num = (int)buffer[0] * 256 + (int)buffer[1];
			int num2 = (int)buffer[2] * 256 + (int)buffer[3];
			long num3 = (long)(num * num2);
			WriteableBitmap writeableBitmap = new WriteableBitmap(num, num2);
			int num4 = 4;
			int num5 = 0;
			while ((long)num5 < num3)
			{
				int num6 = (int)buffer[num4++];
				num6 = (num6 << 8 | (int)buffer[num4++]);
				num6 = (num6 << 8 | (int)buffer[num4++]);
				num6 = (num6 << 8 | (int)buffer[num4++]);
				writeableBitmap.get_Pixels()[num5] = num6;
				num5++;
			}
			return writeableBitmap;
		}
		public static Dictionary<string, NinePatchSrc[]> GetNinePatchDic()
		{
			if (NinePatchHelper.ninePatchDic == null)
			{
				NinePatchHelper.ninePatchDic = new Dictionary<string, NinePatchSrc[]>();
			}
			return NinePatchHelper.ninePatchDic;
		}
		public static void SaveNinePatchSrc(string string_0, NinePatchSrc[] ninePatchSrc)
		{
			NinePatchHelper.GetNinePatchDic().set_Item(string_0, ninePatchSrc);
		}
		public static NinePatchSrc[] GetNinePatchSrc(string string_0)
		{
			if (!NinePatchHelper.GetNinePatchDic().ContainsKey(string_0))
			{
				return null;
			}
			return NinePatchHelper.GetNinePatchDic().get_Item(string_0);
		}
	}
}
